<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>iframeio_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_net_iframeio_test.js.html">iframeio_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.net.IframeIoTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.net.IframeIoTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.debug&#39;);
<a name="line19"></a>goog.require(&#39;goog.debug.DivConsole&#39;);
<a name="line20"></a>goog.require(&#39;goog.debug.LogManager&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom&#39;);
<a name="line22"></a>goog.require(&#39;goog.events&#39;);
<a name="line23"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line24"></a>goog.require(&#39;goog.log&#39;);
<a name="line25"></a>goog.require(&#39;goog.log.Level&#39;);
<a name="line26"></a>goog.require(&#39;goog.net.IframeIo&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.events&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.events.Event&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line30"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line31"></a>
<a name="line32"></a>// MANUAL TESTS - The tests should be run in the browser from the Closure Test
<a name="line33"></a>// Server
<a name="line34"></a>
<a name="line35"></a>// Set up a logger to track responses
<a name="line36"></a>goog.debug.LogManager.getRoot().setLevel(goog.log.Level.INFO);
<a name="line37"></a>var logconsole;
<a name="line38"></a>var testLogger = goog.log.getLogger(&#39;test&#39;);
<a name="line39"></a>
<a name="line40"></a>function setUpPage() {
<a name="line41"></a>  var logconsole = new goog.debug.DivConsole(document.getElementById(&#39;log&#39;));
<a name="line42"></a>  logconsole.setCapturing(true);
<a name="line43"></a>}
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/** Creates an iframeIo instance and sets up the test environment */
<a name="line47"></a>function getTestIframeIo() {
<a name="line48"></a>  logconsole.addSeparator();
<a name="line49"></a>  logconsole.getFormatter().resetRelativeTimeStart();
<a name="line50"></a>
<a name="line51"></a>  var io = new goog.net.IframeIo();
<a name="line52"></a>  io.setErrorChecker(checkForError);
<a name="line53"></a>
<a name="line54"></a>  goog.events.listen(io, &#39;success&#39;, onSuccess);
<a name="line55"></a>  goog.events.listen(io, &#39;error&#39;, onError);
<a name="line56"></a>  goog.events.listen(io, &#39;ready&#39;, onReady);
<a name="line57"></a>
<a name="line58"></a>  return io;
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>/**
<a name="line63"></a> * Checks for error strings returned by the GSE and error variables that
<a name="line64"></a> * the Gmail server and GFE set on certain errors.
<a name="line65"></a> */
<a name="line66"></a>function checkForError(doc) {
<a name="line67"></a>  var win = goog.dom.getWindow(doc);
<a name="line68"></a>  var text = doc.body.textContent || doc.body.innerText || &#39;&#39;;
<a name="line69"></a>  var gseError = text.match(/([^\n]+)\nError ([0-9]{3})/);
<a name="line70"></a>  if (gseError) {
<a name="line71"></a>    return &#39;(Error &#39; + gseError[2] + &#39;) &#39; + gseError[1];
<a name="line72"></a>  } else if (win.gmail_error) {
<a name="line73"></a>    return win.gmail_error + 700;
<a name="line74"></a>  } else if (win.rc) {
<a name="line75"></a>    return 600 + win.rc % 100;
<a name="line76"></a>  } else {
<a name="line77"></a>    return null;
<a name="line78"></a>  }
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/** Logs the status of an iframeIo object */
<a name="line83"></a>function logStatus(i) {
<a name="line84"></a>  goog.log.fine(testLogger, &#39;Is complete/success/active: &#39; +
<a name="line85"></a>      [i.isComplete(), i.isSuccess(), i.isActive()].join(&#39;/&#39;));
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function onSuccess(e) {
<a name="line89"></a>  goog.log.warning(testLogger, &#39;Request Succeeded&#39;);
<a name="line90"></a>  logStatus(e.target);
<a name="line91"></a>}
<a name="line92"></a>
<a name="line93"></a>function onError(e) {
<a name="line94"></a>  goog.log.warning(testLogger, &#39;Request Errored: &#39; + e.target.getLastError());
<a name="line95"></a>  logStatus(e.target);
<a name="line96"></a>}
<a name="line97"></a>
<a name="line98"></a>function onReady(e) {
<a name="line99"></a>  goog.log.info(testLogger,
<a name="line100"></a>      &#39;Test finished and iframe ready, disposing test object&#39;);
<a name="line101"></a>  e.target.dispose();
<a name="line102"></a>}
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>function simpleGet() {
<a name="line107"></a>  var io = getTestIframeIo();
<a name="line108"></a>  goog.events.listen(io, &#39;complete&#39;, onSimpleTestComplete);
<a name="line109"></a>  io.send(&#39;/iframeio/ping&#39;, &#39;GET&#39;);
<a name="line110"></a>}
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>function simplePost() {
<a name="line114"></a>  var io = getTestIframeIo();
<a name="line115"></a>  goog.events.listen(io, &#39;complete&#39;, onSimpleTestComplete);
<a name="line116"></a>  io.send(&#39;/iframeio/ping&#39;, &#39;POST&#39;);
<a name="line117"></a>}
<a name="line118"></a>
<a name="line119"></a>function onSimpleTestComplete(e) {
<a name="line120"></a>  goog.log.info(testLogger, &#39;ResponseText: &#39; + e.target.getResponseText());
<a name="line121"></a>}
<a name="line122"></a>
<a name="line123"></a>function abort() {
<a name="line124"></a>  var io = getTestIframeIo();
<a name="line125"></a>  goog.events.listen(io, &#39;complete&#39;, onAbortComplete);
<a name="line126"></a>  goog.events.listen(io, &#39;abort&#39;, onAbort);
<a name="line127"></a>  io.send(&#39;/iframeio/ping&#39;, &#39;GET&#39;);
<a name="line128"></a>  io.abort();
<a name="line129"></a>}
<a name="line130"></a>
<a name="line131"></a>function onAbortComplete(e) {
<a name="line132"></a>  goog.log.info(testLogger, &#39;Hmm, request should have been aborted&#39;);
<a name="line133"></a>}
<a name="line134"></a>
<a name="line135"></a>function onAbort(e) {
<a name="line136"></a>  goog.log.info(testLogger, &#39;Request aborted&#39;);
<a name="line137"></a>}
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>function errorGse404() {
<a name="line141"></a>  var io = getTestIframeIo();
<a name="line142"></a>  io.send(&#39;/iframeio/404&#39;, &#39;GET&#39;);
<a name="line143"></a>}
<a name="line144"></a>
<a name="line145"></a>function jsonEcho(method) {
<a name="line146"></a>  var io = getTestIframeIo();
<a name="line147"></a>  goog.events.listen(io, &#39;complete&#39;, onJsonComplete);
<a name="line148"></a>  var data = {&#39;p1&#39;: &#39;x&#39;, &#39;p2&#39;: &#39;y&#39;, &#39;p3&#39;: &#39;z&#39;, &#39;r&#39;: 10};
<a name="line149"></a>  io.send(&#39;/iframeio/jsonecho?q1=a&amp;q2=b&amp;q3=c&amp;r=5&#39;, method, false, data);
<a name="line150"></a>}
<a name="line151"></a>
<a name="line152"></a>function onJsonComplete(e) {
<a name="line153"></a>  goog.log.info(testLogger, &#39;ResponseText: &#39; + e.target.getResponseText());
<a name="line154"></a>  var json = e.target.getResponseJson();
<a name="line155"></a>  goog.log.info(testLogger,
<a name="line156"></a>      &#39;ResponseJson:\n&#39; + goog.debug.deepExpose(json, true));
<a name="line157"></a>}
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>function sendFromForm() {
<a name="line162"></a>  var io = getTestIframeIo();
<a name="line163"></a>  goog.events.listen(io, &#39;success&#39;, onUploadSuccess);
<a name="line164"></a>  goog.events.listen(io, &#39;error&#39;, onUploadError);
<a name="line165"></a>  io.sendFromForm(document.getElementById(&#39;uploadform&#39;));
<a name="line166"></a>}
<a name="line167"></a>
<a name="line168"></a>function onUploadSuccess(e) {
<a name="line169"></a>  goog.log.log(testLogger, goog.log.Level.SHOUT, &#39;Upload Succeeded&#39;);
<a name="line170"></a>  goog.log.info(testLogger, &#39;ResponseText: &#39; + e.target.getResponseText());
<a name="line171"></a>}
<a name="line172"></a>
<a name="line173"></a>function onUploadError(e) {
<a name="line174"></a>  goog.log.log(testLogger, goog.log.Level.SHOUT, &#39;Upload Errored&#39;);
<a name="line175"></a>  goog.log.info(testLogger, &#39;ResponseText: &#39; + e.target.getResponseText());
<a name="line176"></a>}
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>function redirect1() {
<a name="line180"></a>  var io = getTestIframeIo();
<a name="line181"></a>  io.send(&#39;/iframeio/redirect&#39;, &#39;GET&#39;);
<a name="line182"></a>}
<a name="line183"></a>
<a name="line184"></a>function redirect2() {
<a name="line185"></a>  var io = getTestIframeIo();
<a name="line186"></a>  io.send(&#39;/iframeio/move&#39;, &#39;GET&#39;);
<a name="line187"></a>}
<a name="line188"></a>
<a name="line189"></a>function badUrl() {
<a name="line190"></a>  var io = getTestIframeIo();
<a name="line191"></a>  io.send(&#39;http://news.bbc.co.uk&#39;, &#39;GET&#39;);
<a name="line192"></a>}
<a name="line193"></a>
<a name="line194"></a>function localUrl1() {
<a name="line195"></a>  var io = getTestIframeIo();
<a name="line196"></a>  goog.events.listen(io, &#39;complete&#39;, onLocalSuccess);
<a name="line197"></a>  io.send(&#39;c:\test.txt&#39;, &#39;GET&#39;);
<a name="line198"></a>}
<a name="line199"></a>
<a name="line200"></a>function localUrl2() {
<a name="line201"></a>  var io = getTestIframeIo();
<a name="line202"></a>  goog.events.listen(io, &#39;success&#39;, onLocalSuccess);
<a name="line203"></a>  io.send(&#39;//test.txt&#39;, &#39;GET&#39;);
<a name="line204"></a>}
<a name="line205"></a>
<a name="line206"></a>function onLocalSuccess(e) {
<a name="line207"></a>  goog.log.info(testLogger,
<a name="line208"></a>      &#39;The file was found:\n&#39; + e.target.getResponseText());
<a name="line209"></a>}
<a name="line210"></a>
<a name="line211"></a>function getServerTime(noCache) {
<a name="line212"></a>  var io = getTestIframeIo();
<a name="line213"></a>  goog.events.listen(io, &#39;success&#39;, onTestCacheSuccess);
<a name="line214"></a>  io.send(&#39;/iframeio/datetime&#39;, &#39;GET&#39;, noCache);
<a name="line215"></a>}
<a name="line216"></a>
<a name="line217"></a>function onTestCacheSuccess(e) {
<a name="line218"></a>  goog.log.info(testLogger, &#39;Date reported: &#39; + e.target.getResponseText());
<a name="line219"></a>}
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>function errorGmail() {
<a name="line223"></a>  var io = getTestIframeIo();
<a name="line224"></a>  goog.events.listen(io, &#39;error&#39;, onGmailError);
<a name="line225"></a>  io.send(&#39;/iframeio/gmailerror&#39;, &#39;GET&#39;);
<a name="line226"></a>}
<a name="line227"></a>
<a name="line228"></a>function onGmailError(e) {
<a name="line229"></a>  goog.log.info(testLogger, &#39;Gmail error: &#39; + e.target.getLastError());
<a name="line230"></a>}
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>function errorGfe() {
<a name="line234"></a>  var io = getTestIframeIo();
<a name="line235"></a>  goog.events.listen(io, &#39;error&#39;, onGfeError);
<a name="line236"></a>  io.send(&#39;/iframeio/gfeerror&#39;, &#39;GET&#39;);
<a name="line237"></a>}
<a name="line238"></a>
<a name="line239"></a>function onGfeError(e) {
<a name="line240"></a>  goog.log.info(testLogger, &#39;GFE error: &#39; + e.target.getLastError());
<a name="line241"></a>}
<a name="line242"></a>
<a name="line243"></a>
<a name="line244"></a>
<a name="line245"></a>function incremental() {
<a name="line246"></a>  var io = getTestIframeIo();
<a name="line247"></a>  io.send(&#39;/iframeio/incremental&#39;, &#39;GET&#39;);
<a name="line248"></a>}
<a name="line249"></a>
<a name="line250"></a>window[&#39;P&#39;] = function(iframe, data) {
<a name="line251"></a>  var iframeIo = goog.net.IframeIo.getInstanceByName(iframe.name);
<a name="line252"></a>  goog.log.info(testLogger, &#39;Data recieved - &#39; + data);
<a name="line253"></a>};
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>function postForm() {
<a name="line257"></a>  var io = getTestIframeIo();
<a name="line258"></a>  goog.events.listen(io, &#39;complete&#39;, onJsonComplete);
<a name="line259"></a>  io.sendFromForm(document.getElementById(&#39;testfrm&#39;));
<a name="line260"></a>}
<a name="line261"></a>// UNIT TESTS - to be run via the JsUnit testRunner
<a name="line262"></a>
<a name="line263"></a>// TODO(user): How to unit test all of this?  Creating a MockIframe could
<a name="line264"></a>// help for the IE code path, but since the other browsers require weird
<a name="line265"></a>// behaviors this becomes very tricky.
<a name="line266"></a>
<a name="line267"></a>
<a name="line268"></a>function testGetForm() {
<a name="line269"></a>  var frm1 = goog.net.IframeIo.getForm_;
<a name="line270"></a>  var frm2 = goog.net.IframeIo.getForm_;
<a name="line271"></a>  assertEquals(frm1, frm2);
<a name="line272"></a>}
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>function testAddFormInputs() {
<a name="line276"></a>  var form = document.createElement(&#39;form&#39;);
<a name="line277"></a>  goog.net.IframeIo.addFormInputs_(form, {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3});
<a name="line278"></a>  var inputs = form.getElementsByTagName(&#39;input&#39;);
<a name="line279"></a>  assertEquals(3, inputs.length);
<a name="line280"></a>  for (var i = 0; i &lt; inputs.length; i++) {
<a name="line281"></a>    assertEquals(&#39;hidden&#39;, inputs[i].type);
<a name="line282"></a>    var n = inputs[i].name;
<a name="line283"></a>    assertEquals(n == &#39;a&#39; ? &#39;1&#39; : n == &#39;b&#39; ? &#39;2&#39; : &#39;3&#39;, inputs[i].value);
<a name="line284"></a>  }
<a name="line285"></a>}
<a name="line286"></a>
<a name="line287"></a>function testNotIgnoringResponse() {
<a name="line288"></a>  // This test can&#39;t run in IE because we can&#39;t forge the check for
<a name="line289"></a>  // iframe.readyState = &#39;complete&#39;.
<a name="line290"></a>  if (goog.userAgent.IE) {
<a name="line291"></a>    return;
<a name="line292"></a>  }
<a name="line293"></a>  var iframeIo = new goog.net.IframeIo();
<a name="line294"></a>  iframeIo.send(&#39;about:blank&#39;);
<a name="line295"></a>  // Simulate the frame finishing loading.
<a name="line296"></a>  goog.testing.events.fireBrowserEvent(new goog.testing.events.Event(
<a name="line297"></a>      goog.events.EventType.LOAD, iframeIo.getRequestIframe()));
<a name="line298"></a>  assertTrue(iframeIo.isComplete());
<a name="line299"></a>}
<a name="line300"></a>
<a name="line301"></a>function testIgnoreResponse() {
<a name="line302"></a>  var iframeIo = new goog.net.IframeIo();
<a name="line303"></a>  iframeIo.setIgnoreResponse(true);
<a name="line304"></a>  iframeIo.send(&#39;about:blank&#39;);
<a name="line305"></a>  // Simulate the frame finishing loading.
<a name="line306"></a>  goog.testing.events.fireBrowserEvent(new goog.testing.events.Event(
<a name="line307"></a>      goog.events.EventType.LOAD, iframeIo.getRequestIframe()));
<a name="line308"></a>  // Although the request is complete, the IframeIo isn&#39;t paying attention.
<a name="line309"></a>  assertFalse(iframeIo.isComplete());
<a name="line310"></a>}
</pre>


</body>
</html>
